package handlers

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"life-share/models"
)

func Create(c *gin.Context) {
	picture := c.PostForm("picture")
	title := c.PostForm("title")
	content := c.PostForm("content")
	if picture == "" || title == "" || content == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	data := models.Life{
		Model:   gorm.Model{},
		Picture: picture,
		Title:   title,
		Content: content,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}
func List(c *gin.Context) {
	//page, _ := strconv.Atoi(c.Query("page"))
	//size, _ := strconv.Atoi(c.Query("size"))
	//offset := (page - 1) * size
	//if page == 0 || size == 0 {
	//	c.JSON(200, gin.H{
	//		"code": 500,
	//		"msg":  "不能为空",
	//	})
	//	return
	//}
	var life []models.Life
	models.DB /*.Offset(offset).Limit(size)*/ .Find(&life)
	//var total int64
	//models.DB.Model(&models.Life{}).Count(&total)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
		//"total": total,
		"data": life,
	})
	return
}
